package msg

import "gitee.com/shuohe/xutils"

//=============================================================================
// Length Validator
//=============================================================================

type ILengthValidator interface {
	ValidateLength(data interface{}) bool
}

//-----------------------------------------------------------------------------
type FixedLengthValidator struct {
	Len int
}

func (this *FixedLengthValidator) ValidateLength(data interface{}) bool {
	return len(data.([]byte)) == this.Len
}

//-----------------------------------------------------------------------------
type VarLengthValidator struct {
	MinLen int
	MaxLen int
}

func (this *VarLengthValidator) ValidateLength(data interface{}) bool {
	l := len(data.([]byte))
	return l >= this.MinLen && l <= this.MaxLen
}

//=============================================================================
// Format Validator
//=============================================================================

type IFormatValidator interface {
	ValidateFormat(data interface{}) bool
}

//-----------------------------------------------------------------------------
type IntRangeValidator struct {
	Min int64
	Max int64
}

func (this *IntRangeValidator) ValidateFormat(data interface{}) bool {
	i := data.(int64)
	return i >= this.Min && i <= this.Max
}

//-----------------------------------------------------------------------------
type DigitValidator struct {}

func (this *DigitValidator) ValidateFormat(data interface{}) bool {
	bs := data.([]byte)
	for _, b := range bs {
		if !xutils.IsDigit(b) {
			return false
		}
	}
	return true
}

//-----------------------------------------------------------------------------
type AlphaDigitValidator struct {}

func (this *AlphaDigitValidator) ValidateFormat(data interface{}) bool {
	bs := data.([]byte)
	for _, b := range bs {
		if !xutils.IsAlphaDigit(b) {
			return false
		}
	}
	return true
}

//-----------------------------------------------------------------------------
type PrintableValidator struct {}

func (this *PrintableValidator) ValidateFormat(data interface{}) bool {
	bs := data.([]byte)
	for _, b := range bs {
		if !xutils.IsPrintable(b) {
			return false
		}
	}

	return true
}